/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.cux.workflow.dao.hibernate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.redragon.framework.annotation.Cache;
import org.redragon.framework.annotation.Permissions;
import org.redragon.framework.annotation.Permissions.PermissionType;
import org.redragon.framework.annotation.SqlParam;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.DaoCRUDUtil;
import com.erp.cux.workflow.dao.CuxWorkflowRecordDao;
import com.erp.cux.workflow.dao.model.CuxWorkflowConfHead;
import com.erp.cux.workflow.dao.model.CuxWorkflowRecord;
import com.erp.cux.workflow.dao.model.CuxWorkflowRecordCO;

@Repository
public class CuxWorkflowRecordDaoImpl implements CuxWorkflowRecordDao{ 

    //注入DaoSupport工具类
    @Autowired
    private DaoSupport daoSupport;
    
    @Override
    public void insertDataObject(CuxWorkflowRecord obj) {
        this.daoSupport.insertDataTransaction(obj);
    }

    @Override
    public void updateDataObject(CuxWorkflowRecord obj) {
        this.daoSupport.updateDataTransaction(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(CuxWorkflowRecord obj) {
        this.daoSupport.insertOrUpdateDataTransaction(obj);
    }

    @Override
    public void deleteDataObject(CuxWorkflowRecord obj) {
        this.daoSupport.deleteDataTransactionJPA(obj);
    }

    @Override
    public List<CuxWorkflowRecord> getDataObjects() {
        return this.daoSupport.getDataAllObject(CuxWorkflowRecord.class);
    }

    @Override
    public CuxWorkflowRecord getDataObject(int id) {
        return (CuxWorkflowRecord)this.daoSupport.getDataObject(CuxWorkflowRecord.class, id);
    }
    
    @Override
    public CuxWorkflowRecord getDataObject(String code) {
        return null;
    }
    
    @Override
    public List<CuxWorkflowRecord> getDataObjects(CuxWorkflowRecordCO paramObj) {
        String sql = "select c.* from cux_workflow_record c where 1=1";
        
        Map<String, Object> args = new HashMap<String, Object>();
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "billType", "and c.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "billHeadCode", "and c.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "confHeadCode", "and c.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "confLineCode", "and c.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "approvePerson", "and c.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "approveResult", "and c.", args);
        sql = sql + " order by c.record_id";
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("c", CuxWorkflowRecord.class);
        
        return this.daoSupport.selectDataSql(sql, entity, args);
    }
    
    @Override
    public List<CuxWorkflowRecord> getDataObjects(Pages pages) {
        return null;
    }
    
    @Override
    public List<CuxWorkflowRecord> getDataObjects(Pages pages, CuxWorkflowRecordCO paramObj) {
        return null;
    }

    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, CuxWorkflowRecordCO paramObj) {
        return null;
    }
    
    @Override
    @Permissions(PermissionType.DATA_AUTH)
    public List<CuxWorkflowRecord> getDataObjectsForDataAuth(@SqlParam String dataAuthSQL, Pages pages, CuxWorkflowRecordCO paramObj) {
        return null;
    }
    
    @Override
    public List<String> getBillHeadCodesByPerson(String billType, String approvePerson) {
        String sql = "select distinct c.bill_head_code from cux_workflow_record c "
                   + "where c.bill_type = :billType and c.approve_person = :approvePerson "
                   + "order by c.record_id desc";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("billType", billType);
        args.put("approvePerson", approvePerson);
        
        return (List<String>)this.daoSupport.selectDataSqlCount(sql, args);
    }
    
    @Override
    public CuxWorkflowRecord getCurrentRecordByBill(String billType, String billHeadCode) {
        String sql = "select c.* from cux_workflow_record c "
                   + "where c.bill_type = :billType and c.bill_head_code = :billHeadCode "
                   + "order by c.record_id desc";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("billType", billType);
        args.put("billHeadCode", billHeadCode);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("c", CuxWorkflowRecord.class);
        
        List list = this.daoSupport.selectDataSql(sql, entity, args);
        if(list.size()>0) {
            return (CuxWorkflowRecord)list.get(0);
        }
        
        return null;
    }
    
    @Override
    public CuxWorkflowRecord getLastRecordByBill(String billType, String billHeadCode) {
        String sql = "select c.* from cux_workflow_record c "
                   + "where c.bill_type = :billType and c.bill_head_code = :billHeadCode "
                   + "order by c.record_id desc";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("billType", billType);
        args.put("billHeadCode", billHeadCode);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("c", CuxWorkflowRecord.class);
        
        List list = this.daoSupport.selectDataSql(sql, entity, args);
        if(list.size()>1) {
            return (CuxWorkflowRecord)list.get(1);
        }
        
        return null;
    }
    
    @Override
    public void deleteRecordByBill(String billType, String billHeadCode) {
        String sql = "delete from cux_workflow_record where bill_type = :billType and bill_head_code = :billHeadCode";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("billType", billType);
        args.put("billHeadCode", billHeadCode);
        
        this.daoSupport.executeSQLTransaction(sql, args);
    }
    
}
